﻿/*
 * tìm kiếm
 * NOT DONE
 * Remove DA
 * btnchangesearch ném ra form Tìm Kiếm nâng cao
 * btnchitiet ném ra form chitiet
 */ 
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using DTO;
using DA;

namespace GUI.View
{
    public partial class TimKiem : UserControl
    {
        private MyQueryDA myQueryDA;

        private AppCommands _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                btnCancel.Command = newValue.TimKiemCommands.Cancel;
                //buttonDong.Command = newValue.DanhSachSVCommands.Cancel;
            }
            else
            {
                btnCancel.Command = null;
                //buttonDong.Command = null;
            }
        }

        public TimKiem()
        {
            InitializeComponent();
            myQueryDA = new MyQueryDA();
        }

        private void dataGridViewTimKiemKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;

            try
            {
                if (i > -1 && i < dataGridViewTimKiemKhachHang.RowCount - 1)
                {
                    btnChiTiet.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void btnChangeSearch_Click(object sender, EventArgs e)
        {
         
            //HACK btnChangeSearch.Command = _Commands.TimKiemNangCaoCommands.New;
            using (Form f = new Form())
            {
                f.Controls.Add(new TimKiemNangCao());
                f.SetBounds(0, 0, 1000, 660);
                f.FormBorderStyle = FormBorderStyle.FixedDialog;
                f.StartPosition = FormStartPosition.CenterScreen;
                //f.WindowState = FormWindowState.Maximized;
                f.ShowDialog();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string queryMaKhachHang = "";
            if (txtMaKhachHang.Text != "")
            {
                queryMaKhachHang = String.Format("and KHACHHANG.MaKH = '{0}'", txtMaKhachHang.Text.Trim());
            }
            else
            {
                queryMaKhachHang = "";
            }

            string queryHoten;
            if (txtHoTen.Text != "")
            {
                queryHoten = String.Format("and HoTen like N'%{0}%'", txtHoTen.Text.Trim());
            }
            else
            {
                queryHoten = "";
            }
            string query = String.Format("select KHACHHANG.MaKH,HoTen,SoDT,Email,DiaChi,MaDiaOc,TenLoaiDiaOc,DiaChiDiaOc,ViTri from  KHACHHANG inner join DIAOC on KHACHHANG.MaKH=DIAOC.MaKH inner join LOAIDIAOC on DIAOC.MaLoaiDiaOc=LOAIDIAOC.MaLoaiDiaOc {0} {1}", queryMaKhachHang, queryHoten);

            if (myQueryDA.doQuery(query) == null)
            {
                MessageBox.Show("");
                return;
            }

            DataTable dt = myQueryDA.doQuery(query).Tables[0];
            dataGridViewTimKiemKhachHang.DataSource = dt;

            for (int i = 0; i < dataGridViewTimKiemKhachHang.Rows.Count - 1; i++)
            {
                dataGridViewTimKiemKhachHang.Rows[i].Cells[0].Value = i + 1;
            }

            if (dt.Rows.Count == 0)
            {
                ToastNotification.Show(this, "Không tìm thấy khách hàng.", eToastPosition.MiddleCenter);
            }
            btnChiTiet.Enabled = false;
            dataGridViewTimKiemKhachHang.Refresh();
        }

        private void txtHoTen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, e);
            }
        }

        private void txtMaKhachHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, e);
            }
        }

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8 || e.KeyChar != 13))
                e.Handled = true;
            if (e.KeyChar == 8)
                e.Handled = false;
            if (e.KeyChar == (char)Keys.Space)
                e.Handled = false;
            if (e.KeyChar == 39)
                e.Handled = false;
        }

        private void txtMaKhachHang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            try
            {
                int currenColumn = dataGridViewTimKiemKhachHang.CurrentCell.RowIndex;
                ChiTietKhachHangControl.makhachhang = Convert.ToInt32(dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["MaKH"].Value);
                ChiTietKhachHangControl.hoten = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["HoTen"].Value.ToString();
                ChiTietKhachHangControl.sodienthoai = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["SoDT"].Value.ToString();
                ChiTietKhachHangControl.email = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["Email"].Value.ToString();
                ChiTietKhachHangControl.diachi = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["DiaChi"].Value.ToString();
                ChiTietKhachHangControl._madiaoc = Convert.ToInt32(dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["MaDiaOc"].Value);
                //HACK btnChiTiet.Command = _Commands.ChiTietKhachHangCommands.New;
                using (Form f = new Form())
                {
                    f.Controls.Add(new ChiTietKhachHangControl());
                    f.SetBounds(0, 0, 1000, 660);
                    f.FormBorderStyle = FormBorderStyle.FixedDialog;
                    f.StartPosition = FormStartPosition.CenterScreen;
                    //f.WindowState = FormWindowState.Maximized;
                    f.ShowDialog();
                }
            }
            catch
            {
                MessageBox.Show("Chưa Chọn Bệnh Nhân", "Chi Tiết Bệnh Nhân", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            
        }
    }
}
